package pl.edu.mimuw.irs.rcp.controller.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.FileEditorInput;

import pl.edu.mimuw.irs.rcp.controller.editor.workflow.IRSWorkflowEditor;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;

public class RestoreIRSWorkflowHandler extends AbstractHandler {

	// *** Attributes

	// *** Methods
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		if (event != null && event.getTrigger() instanceof Event) {
			Event trigger = (Event)event.getTrigger();
			if (trigger.data instanceof IFile) {
				IFile resource = (IFile)trigger.data;
				FileEditorInput input = new FileEditorInput(resource);
				
				try {
					HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
							.openEditor(input, IRSWorkflowEditor.ID);
				} catch (PartInitException e) {
					Logger.handle(MessageCode.EDITOR_OPEN_PROBLEM, e, IRSWorkflowEditor.ID);
					return null;
				}
			}
		}
		return null;
	}
}
